package cn.xiaolang;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * TODO
 *
 * @author Dean
 * @date 2021-04-08
 */
@Slf4j
public class AbilityProcessor {

    private static Map<String, String> keyMapping = new HashMap<>();
    private static Map<Object, String> valueMapping = new HashMap<>();
    /**
     * key=audioMode_1,value=talk_single
     */
    private static Map<String, String> specialMapping = new HashMap<>();
    private static ObjectMapper mapper = new ObjectMapper();

    @Data
    static class NewValBody {

        private String ver = "";

        private String support;

        public NewValBody(String support) {
            this.support = support;
        }
    }

    static {
        keyMapping.put("model", "model");
        keyMapping.put("serverModel", "");
        keyMapping.put("identifier", "");
        keyMapping.put("firmwareBranch", "firmwareBranch");
        keyMapping.put("baseVersion", "");
        // version==ver ??
        keyMapping.put("version", "ver");
        keyMapping.put("deviceType", "");
        keyMapping.put("usingYiServer", "");
        keyMapping.put("onlineStatusP2p", "");
        keyMapping.put("alertSupport", "motion_track_warning");
        keyMapping.put("sceneSupport", "");
        keyMapping.put("timelapseSupport", "lapse_video");
        keyMapping.put("cloudSupport", "");
        keyMapping.put("controlpannelSupport", "move_control");
        keyMapping.put("presetSupport", "preset");
        keyMapping.put("echoShowSupport", "echo_show_support");
        keyMapping.put("h265Support", "H265");
        keyMapping.put("humanMovingDetectionSupport", "human_detect");
        keyMapping.put("abnormalSoundDetectionSupport", "abnormal_voice");
        keyMapping.put("localPlaybackSpeedAdjust", "local_playback_speed_adjust");
        keyMapping.put("cloudPlaybackSpeedAdjust", "cloud_playback_speed_adjust");
        keyMapping.put("mstarAudioAECSupport", "mstar_audio_aec_support");
        keyMapping.put("voiceInteractionSupport", "voice_cmd");
        keyMapping.put("cloudVideoAiIndexSupport", "cloud_video_aiIndex_support");
        keyMapping.put("babycryDetectionSupport", "baby_cry");
        keyMapping.put("sdcardSupport", "sdcard_support");
        keyMapping.put("powerSupplyType", "battery_info");
        // 0=talk_single n; 1=talk_single y; 2=talk_two y
        keyMapping.put("audioMode", "");
        keyMapping.put("imageReverseSupport", "image_rotation");
        keyMapping.put("motionDetectSupport", "motion_detect");
        keyMapping.put("ldcAdjustSupport", "LDC");
        keyMapping.put("gestureDetectionSupport", "gestures");
        keyMapping.put("nightModeAdjustSupport", "night_mode_adjust_support");
        keyMapping.put("networkMonitorSupport", "network_info");
        keyMapping.put("switchNewtorkSupport", "switch_newtork_support");
        keyMapping.put("roiSupport", "ROI");
        keyMapping.put("motionTrackSupport", "motion_track");
        keyMapping.put("motionCruiseSuppport", "motion_cruise_suppport");
        keyMapping.put("panoramaSupport", "panorama_photo");
        keyMapping.put("alarmRegionSupport", "alarm_region_support");
        keyMapping.put("bindApproach", "");
        keyMapping.put("timeSwitchSupport", "time_poweron");
        keyMapping.put("platform", "");
        keyMapping.put("pirAlertSupport", "pir");
        keyMapping.put("soundBinding", "voice_broadcast");
        keyMapping.put("wifi5GSupport", "wifi_5g_support");
        keyMapping.put("alarmRingSupport", "voice_warning");
        keyMapping.put("faceDetectionSupport", "face_detection_support");
        keyMapping.put("babyCamSupport", "baby_cam_support");
        keyMapping.put("Speed2XSupport", "speed_2x_support");
        keyMapping.put("alertReserveDays", "");

        valueMapping.put(0, "n");
        valueMapping.put(1, "y");
        valueMapping.put(2, "y");

        specialMapping.put("audioMode", "talk_single,talk_two");
        specialMapping.put("audioMode_0", "talk_single");
        specialMapping.put("audioMode_1", "talk_single");
        specialMapping.put("audioMode_2", "talk_two");
    }


    public static void main(String[] args) throws IOException {
        String targetJson = "{\n" +
                "    \"Speed2XSupport\": 0, \n" +
                "    \"abnormalSoundDetectionSupport\": 0, \n" +
                "    \"alarmRegionSupport\": 1, \n" +
                "    \"alarmRingSupport\": 1, \n" +
                "    \"alertReserveDays\": 7, \n" +
                "    \"alertSupport\": 1, \n" +
                "    \"audioMode\": 1, \n" +
                "    \"babyCamSupport\": 0, \n" +
                "    \"babycryDetectionSupport\": 0, \n" +
                "    \"baseVersion\": 0, \n" +
                "    \"bindApproach\": 0, \n" +
                "    \"cloudPlaybackSpeedAdjust\": 0, \n" +
                "    \"cloudSupport\": 1, \n" +
                "    \"cloudVideoAiIndexSupport\": 0, \n" +
                "    \"controlpannelSupport\": 0, \n" +
                "    \"deviceType\": 1, \n" +
                "    \"echoShowSupport\": 1, \n" +
                "    \"faceDetectionSupport\": 0, \n" +
                "    \"firmwareBranch\": \"familymonitor-d21bg\", \n" +
                "    \"gestureDetectionSupport\": 0, \n" +
                "    \"h265Support\": 0, \n" +
                "    \"humanMovingDetectionSupport\": 1, \n" +
                "    \"identifier\": \"\", \n" +
                "    \"imageReverseSupport\": 1, \n" +
                "    \"ldcAdjustSupport\": 0, \n" +
                "    \"localPlaybackSpeedAdjust\": 1, \n" +
                "    \"model\": \"d21bg\", \n" +
                "    \"motionCruiseSuppport\": 0, \n" +
                "    \"motionDetectSupport\": 1, \n" +
                "    \"motionTrackSupport\": 0, \n" +
                "    \"mstarAudioAECSupport\": 0, \n" +
                "    \"networkMonitorSupport\": 1, \n" +
                "    \"nightModeAdjustSupport\": 1, \n" +
                "    \"onlineStatusP2p\": 1, \n" +
                "    \"panoramaSupport\": 0, \n" +
                "    \"pirAlertSupport\": 1, \n" +
                "    \"platform\": 0, \n" +
                "    \"powerSupplyType\": 1, \n" +
                "    \"presetSupport\": 0, \n" +
                "    \"roiSupport\": 0, \n" +
                "    \"sceneSupport\": 0, \n" +
                "    \"sdcardSupport\": 1, \n" +
                "    \"serverModel\": 62, \n" +
                "    \"soundBinding\": 0, \n" +
                "    \"switchNewtorkSupport\": 0, \n" +
                "    \"timeSwitchSupport\": 0, \n" +
                "    \"timelapseSupport\": 0, \n" +
                "    \"usingYiServer\": 1, \n" +
                "    \"version\": 0, \n" +
                "    \"voiceInteractionSupport\": 0, \n" +
                "    \"wifi5GSupport\": 0\n" +
                "}";
        String newJson  = generateNewAbility(targetJson);
        sortNewJson(newJson);
    }

    private static void sortNewJson(String newJson) throws IOException {
        TreeMap<String, Object> inputMap = mapper.readValue(newJson, TreeMap.class);
        System.out.println(mapper.writeValueAsString(inputMap));
    }

    private static String generateNewAbility(String targetJson) throws IOException {
        Map<String, Object> inputMap = mapper.readValue(targetJson, Map.class);

        Map<String, Object> outMap = new HashMap<>();
        for (Map.Entry<String, Object> entry : inputMap.entrySet()) {
            String key = entry.getKey();
            String newKey = keyMapping.get(key);
            if (StringUtils.isEmpty(newKey)) {
                // 特殊key处理
                String specialNewKeyStr = specialMapping.get(key);
                if (StringUtils.hasText(specialNewKeyStr)) {
                    // 特殊key的取值集合
                    Set<String> specialNewKeys = StringUtils.commaDelimitedListToSet(specialNewKeyStr);
                    for (String specialNewKey : specialNewKeys) {
                        // 匹配取值，则设定为Y
                        if (specialNewKey.equals(specialMapping.get(key + "_" + entry.getValue()))) {
                            outMap.put(specialNewKey, new NewValBody(valueMapping.get(1)));
                        }else {
                            outMap.put(specialNewKey, new NewValBody(valueMapping.get(0)));
                        }
                    }
                    continue;
                } else {
                    log.warn("key:{} not found mapping", key);
                    continue;
                }
            }
            Object value = entry.getValue();
            String newVal = valueMapping.get(value);
            if (StringUtils.isEmpty(newVal)) {
                log.warn("key:{}==value:{} not found mapping", key, value);
                continue;
            }
            outMap.put(newKey, new NewValBody(newVal));
        }
        String newJson = mapper.writeValueAsString(outMap);
        System.out.println(newJson);
        return newJson;
    }
}
